package com.fmdj.dr.controller;
import cn.hutool.core.bean.BeanUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.common.util.PageUtils;
import com.fmdj.dr.controller.form.*;
import com.fmdj.dr.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController", description = "司机端模块的接口")
@Slf4j
public class DriverController {

    @Autowired
    private DriverService driverService;

    @PostMapping("/registerDriver")
    @Operation(summary = "司机注册接口")
    public CommonResult registerDriver(@RequestBody @Valid RegisterDriverForm form) {
        //将前端传来的参数转化成map类型
        Map<String, Object> map = BeanUtil.beanToMap(form);

        //从service层获取司机id
        String driverId = driverService.registerDriver(map);

        //返回给远程调用处
        return CommonResult.ok().put(CommonResult.RETURN_USER_ID, driverId);
    }

    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新司机实名认证信息接口")
    public CommonResult updateDriverAuth(@RequestBody @Valid UpdateDriverAuthForm form) {
        Map<String, Object> map = BeanUtil.beanToMap(form);

        int rows = driverService.updateDriverAuth(map);

        return CommonResult.ok().put(CommonResult.RETURN_ROW, rows);
    }

    @PostMapping("/login")
    @Operation(summary = "司机登录")
    public CommonResult login(@RequestBody @Valid LoginForm form) {

        HashMap loginResultMap = driverService.login(form.getCode());
        //返回给远程调用处
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, loginResultMap);
    }

    @PostMapping("/selectDriverInfo")
    @Operation(summary = "查询司机个人信息")
    public CommonResult selectDriverInfo(@RequestBody @Valid SelectDriverInfoForm form) {
        HashMap hashMap = driverService.selectDriverInfo(form.getDriverId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/selectDriverInfoById")
    @Operation(summary = "查询司机实名认证的信息")
    public CommonResult selectDriverInfoById(@RequestBody @Valid SelectDriverAuthForm form) {
        Long driverId = form.getDriverId();
        HashMap hashMap = driverService.selectDriverInfoById(driverId);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/selectDriverByPage")
    @Operation(summary = "司机的分页查询")
    public CommonResult selectDriverPage(@RequestBody @Valid SelectDriverByPageForm form) {
        Map<String, Object> map = BeanUtil.beanToMap(form);
        PageUtils pageUtils = driverService.selectDriverByPage(map);
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, pageUtils);
    }

    @PostMapping("/selectDriverAuthInfo")
    @Operation(summary = "PC端查询司机认证信息")
    public CommonResult selectDriverAuthInfo(@RequestBody @Valid SelectDriverAuthInfoForm form) {
        HashMap hashMap = driverService.selectDriverAuthInfo(form.getDriverId());
        return CommonResult.ok().put(CommonResult.DRIVER_AUTH_INFO, hashMap);
    }

    @PostMapping("/updateDriverRealAuth")
    @Operation(summary = "更新司机实名认证")
    public CommonResult updateDriverRealAuth(@RequestBody @Valid UpdateDriverRealAuthForm form) {
        int rows = driverService.updateDriverRealAuth(BeanUtil.beanToMap(form));
        return CommonResult.ok().put(CommonResult.RETURN_ROW, rows);
    }

    @PostMapping("/searchDriverBriefInfo")
    @Operation(summary = "查询司机简明信息")
    public CommonResult searchDriverBriefInfo(@RequestBody @Valid SearchDriverBriefInfoForm form) {
        HashMap hashMap = driverService.searchDriverBriefInfo(form.getDriverId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

    @PostMapping("/searchDriverOpenId")
    @Operation(summary = "查询司机OpenId")
    public CommonResult searchDriverOpenId(@RequestBody @Valid SearchDriverOpenIdForm form) {
        String openId = driverService.searchDriverOpenId(form.getDriverId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, openId);
    }
}
